import { Component } from '@angular/core';
import { NavController, NavParams } from 'ionic-angular';
import { UserService } from '../../../service/userService';
import { HttpService } from '../../../service/httpService';
import { NativeService } from '../../../service/NativeService';
@Component({
  selector: 'page-user-info',
  templateUrl: 'changeUserInfo.html',
})
export class ChangeUserInfoPage {

  private title:string;
  private type:any;
  private url:string;

  private Birthday:string;
  private QQ:string;
  private WeChat:string;
  private Phone:string;
  private Email:string;
  constructor(public navCtrl: NavController,
              public user:UserService,
              public http:HttpService,
              public nat:NativeService,
              public navParams: NavParams) {
    this.type = navParams.get("key");
    this.url = navParams.get("url");
    this.QQ = user.QQ;
    this.WeChat = user.WeChat;
    this.Birthday = user.Birthday;
    this.Phone = user.Phone;
    this.Email = user.Email;
    if(this.type == "生日"){
      this.title = "设置生日";
    }else if(this.type == "QQ"){
      this.title = "设置QQ号";
    }else if(this.type == "微信"){
      this.title = "设置微信";
    }else if(this.type == "手机号"){
      this.title = "修改手机号";
    }else if(this.type == "邮箱"){
      this.title = "修改邮箱";
    }
  }


  submitAction(){


    let map = {};
    if(this.type == "生日"){
      map = {};
    }else if(this.type == "QQ"){
      if(!this.QQ || this.QQ.length <= 0){
        this.nat.showToast("QQ号不能为空");
        return;
      }
      if(this.QQ == this.user.QQ){
        this.nat.showToast("无修改");
        return;
      }
      map = {qq:this.QQ};
    }else if(this.type == "微信"){
      if(!this.WeChat || this.WeChat.length <= 0){
        this.nat.showToast("微信号不能为空");
        return;
      }
      if(this.WeChat == this.user.WeChat){
        this.nat.showToast("无修改");
        return;
      }
      map = {wechat:this.WeChat};
    }else if(this.type == "手机号"){
      if(!this.Phone || this.Phone.length <= 0){
        this.nat.showToast("微信号不能为空");
        return;
      }
      if(this.Phone == this.user.Phone){
        this.nat.showToast("无修改");
        return;
      }
      map = {phone:this.Phone};
    }else if(this.type == "邮箱"){
      if(!this.Email || this.Email.length <= 0){
        this.nat.showToast("微信号不能为空");
        return;
      }
      if(this.Email == this.user.Email){
        this.nat.showToast("无修改");
        return;
      }
      map = {email:this.Email};
    }else {
      return;
    }
    let self = this;
    this.nat.showLoading("修改中...");
    this.http.get(this.url,map,function (res,err) {
      self.nat.hideLoading();
      if(!err){
        self.user.WeChat = self.WeChat;
        self.user.QQ = self.QQ;
        self.user.Birthday = self.Birthday;
        self.user.Phone = self.Phone;
        self.user.Email = self.Email;
        self.nat.showToast("修改成功");
        self.navCtrl.pop();
      }
    })


  }

}
